<?php
// +----------------------------------------------------------------------
// | RandomPosts(随机日志
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @copyright
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author Haijun Wu <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: RandomPostsWidget.class.php 100 2011-04-22 09:53:26Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


class RandomPostsWidget extends Widget {

	/**
	 * 构造随机日志
	 * 
	 */
	public function render($data) {
	    $output = '';
		$numPosts = C('random_posts_num') ? C('random_posts_num') : 5;
		$cat_id   = C('random_posts_cat') ? C('random_posts_cat') : '';
		$start = $this->ara_microtime_float();
		$output .= '<!-- Random Posts: START -->'."\n";
		$output .= " <li class=\"widget random_posts dbx-box\">\n";
		$output .= "  <h3 class=\"dbx-handle\">Random Posts</h3>\n";
		$output .= "   <ul class='dbx-content'>\n";
		$output .= $this->random_posts($numPosts,$cat_id);
		$output .= "   </ul>\n";
		$output .= " </li>\n";
		$output .= ' <!-- Random Posts: END -->'."\n";
		$end = $this->ara_microtime_float();
		$output .= ' <!-- Time taken for the 2 queries to complete is '.($end - $start).' seconds -->'."\n";
		return $output;
	}
	
	protected function ara_microtime_float() {
		list($usec, $sec) = explode(" ", microtime());
		return ((float)$usec + (float)$sec);
	}
	
	/**
	 * 获取随机日志
	 * @param str			原字符串
	 * @param numPosts	    日志数
	 * @param category	    分类id
	 */
	protected function random_posts($numPosts = '5',$cat_id = '') {
	
		$string_to_echo = '';
	
		//查询过滤
		$map  =	 array();		
		$map['posts_status'] = 1; 
		$map['posts_draft'] = 1;
		$map['posts_type'] = 1;
		
		$posts  =  M('Posts');
		
		if ($cat_id != '') {
			$map['category_id'] = $cat_id;	
		}
		
		$p_id = $posts->where($map)->field('posts_id')->findAll();
		$luckyPosts = (array) array_rand($p_id,($numPosts > count($p_id) ? count($p_id) : $numPosts));	
		$sql =	"";
		foreach ($luckyPosts as $k=>$id) {
			if ($notfirst) $sql .= " OR";
			else $sql .= " (";
			$sql .= " posts_id = ".$p_id[$id]['posts_id'];
			$notfirst = true;
		}
		$sql .= ')';
		
		$rand_posts = $posts->where($sql)->field('posts_id,posts_title,posts_url,posts_addtime')->findAll();
		
		if ($rand_posts) {
			foreach ($rand_posts as $item) {
				$posts_results[] = array('title'=>str_replace('"','',stripslashes($item['posts_title'])),
									'link'=>__APP__.'/Post/'.$item['posts_url'],'time'=>date('d/m/Y',$item['posts_addtime'])
									);
			}
			for ($x=0; $x<count($posts_results); $x++ ) {
				$string_to_echo .= "    <li><span class=\"sidedate\">".$posts_results[$x]['time']."</span>";
				$string_to_echo	.= '<a href="'.$posts_results[$x]['link'].'">'.$posts_results[$x]['title'].'</a>';
				$string_to_echo .= "</li>\n";
			}
			return $string_to_echo;
		} else {
			return false;
		}
	}
	
}

?>
